package com.spheresh.async;



import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;


public abstract class AsyncTaskActivity extends Activity implements AsyncTaskInterface {

	public AsyncTaskApp task;

	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		AsyncTaskApp task = (AsyncTaskApp) getLastNonConfigurationInstance();
		if (task != null) {
			task.attach(this);
		}

		// Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && getMetaData("android.support.PARENT_ACTIVITY") != null) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
		
	}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            
    }

    @Override
    public AsyncTaskApp onRetainNonConfigurationInstance() {
                return task;
    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }
    private Object getMetaData(String name) {
        try {
            ActivityInfo ai = getPackageManager().getActivityInfo(this.getComponentName(), PackageManager.GET_ACTIVITIES|PackageManager.GET_META_DATA);
            Bundle metaData = ai.metaData; 
            if(metaData != null) {
                Object value = (Object)metaData.get(name);
                return value;
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
	public void execute() {
		task = new AsyncTaskApp(this);
		task.execute();
	}
	public class AsyncTaskApp extends AsyncTask<Void, Void, Void>  {

		private AsyncTaskActivity parent;

		public AsyncTaskApp(AsyncTaskActivity asyncTaskActivity) {
			parent = asyncTaskActivity;
		}

		void detach() {
			parent = null;
	    }
	    
	    void attach(AsyncTaskActivity activity) {
	      this.parent = activity;
	    }
		@Override
	    protected void onPreExecute() {
			parent.onPreExecute();
	    }
		@Override
	    protected void onPostExecute(Void result) {
			parent.onPostExecute(result);
	    }
		
		@Override
		protected Void doInBackground(Void... arg0) {
			parent.doInBackground(arg0);
			return null ;
		}
		
	}
	

}
